package com.song.web;

import com.song.bean.Student;
import com.song.service.impl.GradeServiceImpl;
import com.song.service.impl.StudentServiceImpl;
import com.song.util.PageUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet(urlPatterns = "/Educational/student/studentServlet")
public class StudentServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("update".equals(method)) {
            updatestudent(req, resp);
        } else if ("delete".equals(method)) {
            deleteStudent(req, resp);
        } else if ("insert".equals(method)) {
            insertStudent(req, resp);
        } else if ("find".equals(method)) {
            findbyidstudent(req, resp);
        } else if ("stuno".equals(method)) {
            stunoIsExist(req, resp);
        } else {
            selectStudnet(req, resp);
        }
    }

    public void stunoIsExist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String stuno = req.getParameter("stuno");
        StudentServiceImpl studentService = new StudentServiceImpl();
        Boolean isExist = studentService.stunoIsExist(stuno);
        PrintWriter writer = resp.getWriter();
        if (isExist) {
            writer.println("学号可用");
        } else {
            writer.println("学号已存在");
        }
    }

    /**
     * 查询（模糊查询，分页查询）
     */
    public void selectStudnet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PageUtils pageUtils = new PageUtils();
        //获取信息
        //获取模糊查找信息
        String stuname = req.getParameter("stuname");
        String stuno = req.getParameter("stuno");
        String sex = req.getParameter("sex");
       /* int usex;
        if (sex == null||sex.length() == 0){
            usex = -1;
        }else {
            usex = Integer.parseInt(sex);
        }*/
        //空指针异常
        int usex = (sex == null || sex.length() == 0 ? -1 : Integer.parseInt(sex));

        //分页查找信息
        String pageindex = req.getParameter("pageindex");
        int index = pageindex == null || pageindex.length() == 0 ? 1 : Integer.parseInt(pageindex);


        //调用service
        StudentServiceImpl studentService = new StudentServiceImpl();

        //模糊分页查找
        List<Student> studentList = studentService.getStudent(stuname, stuno, usex, index, pageUtils.getPagesize());

        //学生信息总条数
        Integer total = studentService.getTotalStudent(stuname, stuno, usex);
        //总页码数


        //int totalpage = total % pagesize > 0 ? total / pagesize + 1 : total / pagesize;

        //


        //信息存在，存储信息，跳转页面
        if (studentList != null && studentList.size() > 0) {
            //存信息，响应给前端
            req.setAttribute("list", studentList);
            //模糊查找信息存储
            req.setAttribute("stuname", stuname);
            req.setAttribute("stuno", stuno);
            req.setAttribute("sex", sex);
            //分页信息存储
            //当前页数
            pageUtils.setIndex(index);
            //总信息数
            pageUtils.setTotal(total);
            //总页码数
            req.setAttribute("page", pageUtils);
            req.getRequestDispatcher("list.jsp").forward(req, resp);
        } else {
            resp.getWriter().println("<script>alert('没有信息');location.href='/Educational/student/list.jsp'</script>");
        }
    }

    /**
     * 修改
     *
     * @param req  请求
     * @param resp 响应
     * @throws ServletException
     * @throws IOException
     */
    public void updatestudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取信息
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idnumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        String gid = req.getParameter("gid");
        String stuid = req.getParameter("stuid");

        Student student = new Student();
        student.setStuno(stuno);
        student.setIntroduction(introduction);
        student.setPolitics(politics);
        student.setIdnumber(idnumber);
        student.setProfession(profession);
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setStuname(stuname);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setGid(Integer.parseInt(gid));
        student.setAddress(address);
        student.setStuid(Integer.parseInt(stuid));
        //调用service
        StudentServiceImpl studentService = new StudentServiceImpl();
        Integer integer = studentService.updatetudent(student);
        if (integer > 0) {
            resp.getWriter().println("<script>alert('修改成功');location.href='/Educational/student/studentServlet'</script>");
        } else {
            resp.getWriter().println("<script>alert('修改失败');location.href='/Educational/student/studentServlet?method=update&stuid=" + stuid + "'</script>");
        }
    }

    /**
     * 添加学生
     *
     * @param req  请求
     * @param resp 响应
     * @throws ServletException
     * @throws IOException
     */
    public void insertStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取信息
        String stuno = req.getParameter("stuno");
        String stuname = req.getParameter("stuname");
        String sex = req.getParameter("sex");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String registered = req.getParameter("registered");
        String address = req.getParameter("address");
        String politics = req.getParameter("politics");
        String idnumber = req.getParameter("idnumber");
        String profession = req.getParameter("profession");
        String introduction = req.getParameter("introduction");
        String gid = req.getParameter("gid");

        Student student = new Student();
        student.setStuno(stuno);
        student.setIntroduction(introduction);
        student.setPolitics(politics);
        student.setIdnumber(idnumber);
        student.setProfession(profession);
        student.setSex(Integer.parseInt(sex));
        student.setEmail(email);
        student.setStuname(stuname);
        student.setPhone(phone);
        student.setRegistered(registered);
        student.setGid(Integer.parseInt(gid));
        student.setAddress(address);
        //调用service
        StudentServiceImpl studentService = new StudentServiceImpl();
        int integer = studentService.insertStudent(student);
        if (integer > 0) {
            resp.getWriter().println("<script>alert('添加成功');location.href='/Educational/student/studentServlet'</script>");
        } else {
            resp.getWriter().println("<script>alert('添加失败');location.href='/Educational/student/edit.jsp'</script>");
        }
    }

    /**
     * 删除学生
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void deleteStudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取数据
        String stuid = req.getParameter("stuid");
        //调用service
        StudentServiceImpl studentService = new StudentServiceImpl();
        Integer integer = studentService.deleteStudent(Integer.parseInt(stuid));
        if (integer != null) {
            resp.getWriter().println("<script>alert('删除成功');location.href='/Educational/student/studentServlet'</script>");
        } else {
            resp.getWriter().println("<script>alert('删除失败');location.href='/Educational/student/studentServlet'</script>");
        }
    }

    /**
     * 通过学生ID查询学生信息
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void findbyidstudent(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //后去数据
        String stuid = req.getParameter("stuid");
        //调用service
        //通过id查询学生信息
        StudentServiceImpl studentService = new StudentServiceImpl();
        Student student = studentService.findById(Integer.parseInt(stuid));
        //查找年纪信息
        GradeServiceImpl gradeService = new GradeServiceImpl();
        List grade = gradeService.getGrade();
        req.setAttribute("grade", grade);
        if (student != null) {
            req.setAttribute("stu", student);
            req.getRequestDispatcher("add.jsp").forward(req, resp);
        }
    }
}
